package com.baizhi.shane.config;

import com.baizhi.shane.sys.manager.RedisManager;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 填充器
 * https://blog.csdn.net/weixin_44866272/article/details/116696444
 */

@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Resource
    private RedisManager redisManager;

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill...");
        this.setFieldValByName("createTime", new Date(), metaObject);
        Long userId = this.redisManager.getUserId();
        this.setFieldValByName("createBy", userId, metaObject);
        this.setFieldValByName("delFlag", 0, metaObject);
//		this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill...");
        this.setFieldValByName("updateTime", new Date(), metaObject);
		Long userId = this.redisManager.getUserId();
		this.setFieldValByName("updateBy", userId, metaObject);
    }

}

